package com.hspedu.newData.dmsxl.数组.滑动窗口;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC209长度最小的子数组 {

    // 滑动窗口
    public int minSubArrayLen(int target, int[] nums) {
        int slow = 0;
        int fast = 0;
        int sum = 0;
        int candidate = Integer.MAX_VALUE;
        while (slow < nums.length){
            if (sum < target){
                if (fast == nums.length){
                    break;
                }
                sum += nums[fast++];
            } else{
                candidate = Math.min(candidate,fast - slow);
                sum -= nums[slow++];
            }
        }
        return candidate == Integer.MAX_VALUE ? 0:candidate;
    }

    // todo
    // 二分查找

    @Test
    public void test() {
        // 测试代码
        minSubArrayLen(7,new int[]{2,3,1,2,4,3});
        minSubArrayLen(4,new int[]{1,4,4});
        minSubArrayLen(11,new int[]{1,1,1,1,1,1,1,1});
        minSubArrayLen(11,new int[]{1,2,3,4,5});
    }
}
